package com.fangwei.utilsLibrary.customerview.rounddiskview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import com.fangwei.utils.R;
import java.text.DecimalFormat;

/**
 * 自定义的类似圆盘的view(类似芝麻分的展示)
 *
 * Created by fangwei on 2017/9/11.
 */

public class RoundDiskView extends View {
    private static final String TAG = "RoundDiskView";
    private Context context;
    private int width;//宽度
    private int height;//高度
    private float defaulBorderWidth;//默认的圆弧的宽度

    private float startAngle = 150;//开始角度
    private float endAngle = 240;//结束角度
    private float currentAngle = 0;//当前角度

    private int defaultRoundColor;//默认的圆弧颜色
    private int currentRoundColor;//当前进度的圆弧的颜色
    private int dottedColor;//虚线圆弧的颜色
    private int pointColor;//指示小圆点的颜色
    private float pointRadius;//指示小圆点的半径

    private float maxCount = 100;//最大值
    private float currentCount = 50;//当前进度值
    private ValueAnimator animator;//旋转动画
    private int duration;//动画的时长

    public RoundDiskView(Context context) {
        this(context, null);
    }

    public RoundDiskView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundDiskView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs, defStyleAttr);
    }

    /**
     * view的初始化操作
     * 获取一些自定义的属性
     */
    private void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundDiskView);

        pointRadius = typedArray.getFloat(R.styleable.RoundDiskView_roundDiskViewLittlePointRadius, 7);
        pointColor = typedArray.getColor(R.styleable.RoundDiskView_roundDiskViewLittlePointColor, Color.GRAY);
        dottedColor = typedArray.getColor(R.styleable.RoundDiskView_roundDiskViewDottedColor, Color.GRAY);
        duration = typedArray.getInt(R.styleable.RoundDiskView_roundDiskViewDuration, 1000);
        defaultRoundColor = typedArray.getColor(R.styleable.RoundDiskView_roundDiskViewBackgroud, Color.GRAY);
        defaulBorderWidth = typedArray.getFloat(R.styleable.RoundDiskView_roundDiskViewBorderWidth, 30f);
        currentRoundColor = typedArray.getColor(R.styleable.RoundDiskView_roundDiskViewCurrentColor, Color.RED);
        currentCount = typedArray.getFloat(R.styleable.RoundDiskView_roundDiskViewCurrentCount, 0f);
        if (currentCount > 0) {
            setCurrentCount(currentCount);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.width = w;
        this.height = h;
        invalidate();//请求重绘
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float centerX = (width - 2 * defaulBorderWidth) / 2;
        RectF ovall = new RectF(defaulBorderWidth, defaulBorderWidth, 2 * centerX + defaulBorderWidth, 2 * centerX + defaulBorderWidth);

        drawDefaultRound(canvas, ovall);
        drawCurrentRound(canvas, ovall);
        drawDottedLine(canvas);
        drawLittlePoint(canvas, centerX);
    }


    /**
     * 绘制默认底色的圆弧
     */
    private void drawDefaultRound(Canvas canvas, RectF oval1) {
        Paint paint = new Paint();
        paint.setColor(defaultRoundColor);//设置颜色
        paint.setStrokeCap(Paint.Cap.ROUND);//设置样式
        paint.setStyle(Paint.Style.STROKE);//设置填充样式
        paint.setAntiAlias(true);//设置抗锯齿功能
        paint.setStrokeWidth(defaulBorderWidth);//设置画笔的宽度
        canvas.drawArc(oval1, startAngle, endAngle, false, paint);//绘制灰色的圆弧
    }

    /**
     * 绘制当前进度的圆弧
     */
    private void drawCurrentRound(Canvas canvas, RectF oval1) {
        Paint paint = new Paint();
        paint.setColor(currentRoundColor);
        paint.setStrokeCap(Paint.Cap.ROUND);//设置样式
        paint.setStyle(Paint.Style.STROKE);//设置填充样式
        paint.setAntiAlias(true);//设置抗锯齿功能
        paint.setStrokeWidth(defaulBorderWidth);//设置画笔的宽度
        canvas.drawArc(oval1, startAngle, currentAngle, false, paint);//绘制灰色的圆弧
    }


    /**
     * 绘制圆盘虚线
     */
    private void drawDottedLine(Canvas canvas) {
        float centerX = (width - 2 * defaulBorderWidth) / 2;
        RectF oval1 = new RectF(defaulBorderWidth + 1.5f * defaulBorderWidth, defaulBorderWidth + 1.5f * defaulBorderWidth, 2 * centerX + defaulBorderWidth - 1.5f * defaulBorderWidth, 2 * centerX + defaulBorderWidth - 1.5f * defaulBorderWidth);
        Paint paint = new Paint();
        paint.setColor(dottedColor);//设置颜色
        paint.setStrokeCap(Paint.Cap.ROUND);//设置样式
        paint.setStyle(Paint.Style.STROKE);//设置填充样式
        paint.setAntiAlias(true);//设置抗锯齿功能
        paint.setStrokeWidth(5f);//设置画笔的宽度
        PathEffect effects = new DashPathEffect(new float[]{5, 20, 5, 20}, 0);
        paint.setPathEffect(effects);
        canvas.drawArc(oval1, startAngle, endAngle, false, paint);//绘制灰色的圆弧
    }

    /**
     * 绘制一个指示的小圆点
     */
    private void drawLittlePoint(Canvas canvas, float centerX) {
        canvas.translate(getWidth() / 2, getHeight() / 2);//这时候的画布已经移动到了中心位置
        Paint paintCircle = new Paint();
        paintCircle.setStyle(Paint.Style.FILL);//设置填充样式
        paintCircle.setAntiAlias(true);//抗锯齿功能
        paintCircle.setColor(pointColor);
        canvas.rotate(currentAngle - 30);
        canvas.drawCircle(-centerX + 65, 0, pointRadius, paintCircle);
        canvas.rotate(-currentAngle + 60);
    }

    /**
     * 设置当前的进度
     */
    public void setCurrentCount(float count) {
        DecimalFormat df = new DecimalFormat("0.0");//格式化小数

        float current = Float.parseFloat(df.format(count / maxCount));
        float stopAngle = endAngle * (current > 1 ? 1 : current);

        showAnimation(currentAngle, stopAngle, duration);
    }

    /**
     * 设置动画
     */
    private void showAnimation(float last, final float current, int duration) {
        animator = new ValueAnimator().ofFloat(last, current);
        animator.setDuration(duration);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                currentAngle = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        animator.start();
    }
}
